var socketResult = null; //result

var Callback = null;

function binToAscii(num) {
	var str1 = "";
	if (typeof num != "String") {
		var str = num.toString();
	}
	var Hlong = Math.ceil(str.length / 8);
	for (var i = 0; i < Hlong; i++) {
		str1 += String.fromCharCode(parseInt(str.substring(i * 8, (i + 1) * 8), 2));
	}
	return str1;
}

var initAPI = function() {
	this.onopen = function() {
		console.log("onopen callback function is no assignment");
	};

	this.onerror = function() {
		console.log("onerror callback function is no assignment");
	};

	this.onclose = function() {
		console.log("onclose callback function is no assignment");
	};

	this.Callback = function(result) {
		console.log(result.result);
		console.log("onmessage  callback function is no assignment");
	};
};

function printAPI(init) {
	this.webSocket = null;
	this.initAPI = init;
	//webSocket connect

	this.webSocketConnect = function(url) {
		if (this.webSocket !== null) {
			this.webSocket.close();
		}
		this.webSocket = new WebSocket(url);

		this.webSocket.onopen = this.initAPI.onopen;
		this.webSocket.onerror = this.initAPI.onerror;
		this.webSocket.onclose = this.initAPI.onclose;
		Callback = this.initAPI.Callback;
		this.webSocket.onmessage = function(ev) {
			// console.log(ev)
			socketResult = JSON.parse(ev.data);
			Callback(socketResult);
		};
	};

	//webSocket close
	this.webSocketClose = function() {
		if (this.webSocket !== null) {
			this.webSocket.close();
		} else {
			console.log("Please connect first");
		}
	};

	//EB_Init
	this.EB_Init = function(interval) {
		if (this.webSocket === null) {
			console.log("Please connect first");
		} else {
			if (this.webSocket.readyState === 1) {
				this.webSocket.send(
					'{"dll":"ZQEBSDK","name":"EB_Init","param":["' +
						interval +
						'","COM2","9600"]}'
				);
			} else {
				console.log("connect is not ready");
			}
		}
	};
	//EB_SetZero
	this.EB_SetZero = function() {
		if (this.webSocket === null) {
			console.log("Please connect first");
		} else {
			if (this.webSocket.readyState === 1) {
				this.webSocket.send('{"dll":"ZQEBSDK","name":"EB_SetZero","param":[]}');
			} else {
				console.log("connect is not ready");
			}
		}
	};
	//EB_SetNetWeight
	this.EB_SetNetWeight = function() {
		if (this.webSocket === null) {
			console.log("Please connect first");
		} else {
			if (this.webSocket.readyState === 1) {
				this.webSocket.send(
					'{"dll":"ZQEBSDK","name":"EB_SetNetWeight","param":[]}'
				);
			} else {
				console.log("connect is not ready");
			}
		}
	};
	//EB_ClearTare
	this.EB_ClearTare = function() {
		if (this.webSocket === null) {
			console.log("Please connect first");
		} else {
			if (this.webSocket.readyState === 1) {
				this.webSocket.send(
					'{"dll":"ZQEBSDK","name":"EB_ClearTare","param":[]}'
				);
			} else {
				console.log("connect is not ready");
			}
		}
	};
	//EB_SetTare
	this.EB_SetTare = function(tare) {
		if (this.webSocket === null) {
			console.log("Please connect first");
		} else {
			if (this.webSocket.readyState === 1) {
				this.webSocket.send(
					'{"dll":"ZQEBSDK","name":"EB_SetTare","param":["' + tare + '"]}'
				);
			} else {
				console.log("connect is not ready");
			}
		}
	};
}

export default { printAPI, binToAscii, initAPI };
